﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ProductManagement;

public partial class Modules_Purchase_ucAddAccessory : System.Web.UI.UserControl
{
    Products _accessories = new Products();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["Product_Add/EditAccessory"].ToString() == "Edit")        
            LoadData();        
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Clear();
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (Session["Product_Add/EditAccessory"].ToString() == "Add")
            Save();
        else if (Session["Product_Add/EditAccessory"].ToString() == "Edit")
            Update();

    }
    protected void lnbBack_Click(object sender, EventArgs e)
    {
        Back();
    }

     void LoadData()
    {
        try
        {
            string condition = "AccessoryID = '" + ((string[])Session["Product_IDs"])[0].ToString() + "'";
            DataSet dsAccessory = _accessories.GetData("AccessoriesList", condition);
            txtName.Text = dsAccessory.Tables[0].Rows[0]["AccessoryName"].ToString();
            txtDescription.Text = dsAccessory.Tables[0].Rows[0]["Description"].ToString();
            txtUPC.Text = dsAccessory.Tables[0].Rows[0]["UPC"].ToString();
            txtListPrice.Text = dsAccessory.Tables[0].Rows[0]["ListPrice"].ToString();
            txtSpecification.Text = dsAccessory.Tables[0].Rows[0]["Specification"].ToString();

        }
        catch (Exception exp)
        {
            Session["ERRTITLE"] = "Load Data Failed";
            Session["ERRDET"] = exp.Message;
            Response.Redirect("ErrorPage.aspx");
        }
    }

    void Update()
    {
        Accessory accessory = new Accessory();

        accessory.ID = ((string[])Session["Product_IDs"])[0].ToString();
        accessory.Name = txtName.Text;
        accessory.Description = txtDescription.Text;
        accessory.UPC = txtUPC.Text;
        accessory.ListPrice = txtListPrice.Text;
        accessory.Specification = txtSpecification.Text;

        _accessories.EditAccessory(accessory);
        Back();
    }

    void Save()
    {
        try
        {
            Accessory accessory = new Accessory();

            accessory.Name = txtName.Text;
            accessory.Description = txtDescription.Text;
            accessory.UPC = txtUPC.Text;
            accessory.ListPrice = txtListPrice.Text;
            accessory.Specification = txtSpecification.Text;

            if (_accessories.AddAccessory (accessory))
                {
                    Response.Write("<Script>alert('Added Successfully !')</Script>");
                    Clear();
                }
                else
                    Response.Write("<Script>alert('Already Exist !')</Script>");
        }
        catch (Exception exp)
        {
            Session["ERRTITLE"] = "Add Accessory Failed";
            Session["ERRDET"] = exp.Message;
            Response.Redirect("ErrorPage.aspx");
        }
    }

    
    void Clear()
    {
        txtName.Text = string.Empty;       
        txtDescription.Text = string.Empty;
        txtUPC.Text = string.Empty;       
        txtListPrice.Text = string.Empty;
        txtSpecification.Text = string.Empty;
    }

    void Back()
    {
        Session["UserManager_Control"] = "Production/ucAccessoriesList.ascx";
        Response.Redirect("BusinessProcessAutomation.aspx");
    }
    
}
